﻿#region

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using Styx;
using Styx.Helpers;
using Styx.Plugins.PluginClass;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

#endregion

namespace OrePerHour.Plugins
{
    public class OresPerHour : HBPlugin
    {
        private const int MinutesBetweenCheck = 5;
        private readonly Stopwatch _stopwatch = new Stopwatch();
        private DateTime _lastchecked;
        private Dictionary<string, int> _oreCounter = new Dictionary<string, int>();

        public override string Name
        {
            get { return "Ores Per Hour"; }
        }

        public override string Author
        {
            get { return "Narayan"; }
        }

        public override Version Version
        {
            get { return new Version(1, 0, 0, 0); }
        }

        public override string ButtonText
        {
            get { return "Show OPH"; }
        }

        public override void Pulse()
        {
            if (!_stopwatch.IsRunning)
            {
                _stopwatch.Start();
                _lastchecked = DateTime.Now;
            }
            else if (DateTime.Now - _lastchecked  <= new TimeSpan(0, 0, MinutesBetweenCheck, 0))
            {
                CalculateOPH();
                _lastchecked = DateTime.Now;
            }
        }

        public override void Initialize()
        {
            Lua.Events.AttachEvent("UNIT_INVENTORY_CHANGED", GetOre);
        }

        public override void Dispose()
        {
            Lua.Events.DetachEvent("UNIT_INVENTORY_CHANGED", GetOre);
            _stopwatch.Reset();
        }

        public void GetOre(object sender, LuaEventArgs e)
        {
            _oreCounter = new Dictionary<string, int>();
            foreach (WoWItem item in StyxWoW.Me.BagItems.Where(item => item.Name.Contains("Ore")))
            {
                if (_oreCounter.ContainsKey(item.Name))
                {
                    _oreCounter[item.Name] = _oreCounter[item.Name] + (Int32) item.StackCount;
                }
                else
                {
                    _oreCounter.Add(item.Name, (Int32) item.StackCount);
                }
            }
        }

        public void CalculateOPH()
        {
            // Get total number of ore
            long totalOre = _oreCounter.Aggregate(0, (current, i) => current + i.Value);
            // Get time running
            long timerunning = _stopwatch.ElapsedMilliseconds / (1000 * 60 * 60);
            // Get ore per hour
            if (totalOre > 0 && timerunning > 0)
            {
                PrintStats(totalOre / timerunning);
            }
        }

        public void PrintStats(long oph)
        {
            Slog("**** Ore Per Hour ****");
            Slog("Collected:");
            foreach (KeyValuePair<string, int> i in _oreCounter)
            {
                Slog(i.Value + " x " + i.Key);
            }
            Slog("Total: " + oph);
            Slog("**** Ore Per Hour ****");
        }

        public static void Slog(string message)
        {
            Logging.Write(Color.SeaGreen, message);
        }

        public override void OnButtonPress()
        {
            new OPH(_oreCounter, _stopwatch.ElapsedMilliseconds).ShowDialog();
        }
    }
}